home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #14 / Monster Media No. 14 (April 1996) (Monster Media, Inc.).ISO / games_a / wordy410.zip / SUFFIX.C < prev    next >
C/C++ Source or Header  |  1995-12-17  |  7KB  |  241 lines

  1. /**************************************************************************/
  2. /*                             Suffix  Utility                            */
  3. /*                                                                        */
  4. /*                                 M\Cooper                               */
  5. /*                        3425 Chestnut Ridge Rd.                         */
  6. /*                        Grantsville, MD 21536-9801                      */
  7. /*                        --------------------------                      */
  8. /*                        Email:  thegrendel@aol.com                      */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package              */
  11. /*                                                                        */
  12. /**************************************************************************/
  13.  
  14.  
  15. #include <conio.h>
  16. #include "srch.h"
  17.  
  18.  
  19. #define FILE_OPENING_ERROR 3
  20. #define FILENAME_MAXLEN 8
  21. #define CR "\n"
  22. #define FILE_SUFFIX ".suf"
  23. #define MAXLEN 30
  24. #define LINE_LEN 80
  25. #define NOARGS 1
  26. #define INCREMENT 1
  27. #define WD 10
  28. #define SPACE ' '
  29.  
  30. #define BUFFERSIZE 8192
  31.  
  32. char ad[] =
  33. "SUFFIX utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  34.  
  35. typedef enum { FALSE, TRUE } Boolean;
  36.  
  37. void getword( char *lset, char *filenam );
  38. void center( char *strng );
  39. Boolean wordtest( char *lset, char *wd, size_t len );
  40.  
  41.  
  42. void main( int argc, char **argv )
  43. {
  44.  
  45.    char letterset[ MAXLEN ],
  46.         filename [ MAXLEN ];
  47.  
  48.  
  49.  
  50.      if( argc == NOARGS )
  51.         {
  52.         clrscr();
  53.         puts("Enter a LETTERSET to test ... ");
  54.         gets( letterset );
  55.      strcpy( filename, "word.lst" );
  56.         }
  57.   else
  58.         if( argc == NOARGS + 1 )
  59.         {
  60.            strcpy( letterset, *( argv + 1 ) );
  61.         strcpy( filename, "word.lst" );
  62.         }
  63.      else
  64.          {
  65.          strcpy( letterset, *( argv + 1 ) );
  66.          strcpy( filename,  *( argv + 2 ) );
  67.          }
  68.  
  69.      getword( letterset, filename );
  70.  
  71. }
  72.  
  73.  
  74. /**********************************WORDTEST********************************/
  75. /*       Function tests if word is constructible from Letterset           */
  76. /*                 Args in: char *letterset, char *word                   */
  77. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  78. /**************************************************************************/
  79.  
  80. Boolean wordtest( char *letterset, char *word, size_t l_len )
  81. {
  82.     Boolean error_flag;
  83.  
  84.       if( !strncmp( letterset, word + strlen( word ) - 1 - l_len, l_len ) )
  85.          error_flag = TRUE;
  86.       else
  87.          error_flag = FALSE;
  88.         return( error_flag );
  89. }
  90.  
  91. /*************************************************************/
  92. void getword( char *letter_set, char *filename )
  93. {
  94.  
  95.     char    l_set [ MAXLEN ],
  96.         word [ MAXLEN ],
  97.         tempstr [ MAXLEN + 1 ],
  98.         targetfile [ MAXLEN ],
  99.         bar [ LINE_LEN + 1 ],
  100.         double_bar [ LINE_LEN + 1 ],
  101.   msg1 [ WD ],
  102.   msg2 [ WD ];
  103.    size_t l_len;
  104.  
  105.     FILE *fptr,
  106.         *tfile;
  107.     int fnamelen;
  108.     long wcount = 0L;
  109.  
  110.  
  111.       l_len = strlen( letter_set );
  112.        memset( bar, '-', LINE_LEN );
  113.        *( bar + LINE_LEN ) = NULL;
  114.        memset( double_bar, '=', LINE_LEN );
  115.        *( double_bar + LINE_LEN ) = NULL;
  116.  
  117.        /*************opening credits*************/
  118.        clrscr();
  119.        printf( double_bar );
  120.        strcpy( tempstr, ad );
  121.        center ( tempstr );
  122.        printf( tempstr );
  123.        printf( CR );
  124.        printf( double_bar );
  125.        printf( CR );
  126.        /****************************************/
  127.  
  128.  
  129.        strcpy ( l_set, letter_set );
  130. //       strcat ( letter_set, CR );
  131.  
  132.        /*   Create name of file to store derived words in   */
  133.        /*********************************************************/
  134.        fnamelen = strlen( l_set );
  135.        if( fnamelen  > FILENAME_MAXLEN )
  136.           fnamelen = FILENAME_MAXLEN;
  137.        strncpy( targetfile, l_set, fnamelen );
  138.        *( targetfile + fnamelen ) = NULL;
  139.        //NULL-terminate string, so strcat works, ha, ha.
  140.        strcat( targetfile, FILE_SUFFIX );
  141.        /*********************************************************/
  142.  
  143.        if( !( fptr = fopen( filename, "rt" ) ) )
  144.          {
  145.          printf( "\7\7\7Cannot open Wordfile!" );
  146.          exit( FILE_OPENING_ERROR );
  147.          }
  148.       if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
  149.          exit( FILE_OPENING_ERROR + 1 );
  150.  
  151.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  152.          {
  153.          printf( "\7\7\7Cannot open file to save words in!" );
  154.          exit ( FILE_OPENING_ERROR + 2 );
  155.          }
  156.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  157.          exit( FILE_OPENING_ERROR + 3 );
  158.  
  159.        /**************'Wait' Message************/
  160.        printf( CR CR );
  161.        printf( "WORKING...\n\n" );
  162.        printf( "This will take a few seconds...\n" );
  163.        printf( "Please be patient.\n\n" );
  164.        printf(
  165. "Now searching 100,000+ word file \nand writing file of valid words suffixed -%s-.\n\n",
  166.             letter_set );
  167.        /*****************************************/
  168.  
  169.  
  170.  
  171.  
  172.  
  173.        sprintf( tempstr, "Word(s) generated with suffix: %s\n", strupr( l_set ) );
  174.        center( tempstr );
  175.        fprintf( tfile, double_bar );
  176.       fprintf( tfile, CR );
  177.        fprintf( tfile, tempstr );
  178.        fprintf( tfile, double_bar );
  179.        fprintf( tfile, CR );
  180.  
  181.  
  182.          /*********************Main Loop*************/     
  183.           while( fgets( word, MAXLEN, fptr ) != NULL )
  184.  
  185.             if( wordtest( letter_set, word, l_len ) )
  186.                {
  187.                fprintf( tfile, "%s", word );
  188.                wcount++;
  189.                }
  190.           /*******************************************/
  191.  
  192.       if( wcount == INCREMENT )
  193.          {
  194.          strcpy( msg1, "word" );
  195.          strcpy( msg2, "has" );
  196.          }
  197.       else
  198.          {
  199.          strcpy( msg1, "words" );
  200.          strcpy( msg2, "have" );
  201.          }
  202.  
  203.  
  204.           fprintf( tfile, bar );
  205.       fprintf( tfile, CR );
  206.           sprintf( tempstr, "%ld %s suffixed %s %s been found.",
  207.                  wcount, msg1, l_set, msg2 );
  208.           center( tempstr );              
  209.           fprintf( tfile, tempstr );
  210.           fprintf( tfile, "\n\n" );
  211.  
  212.           center( ad );
  213.           fprintf( tfile, ad );
  214.  
  215.           fcloseall();
  216.  
  217.           sprintf( tempstr,
  218.                  "The file %s has %ld %s suffixed by %s\7.",
  219.                  targetfile, wcount, msg1, l_set );
  220.           center( tempstr );
  221.           printf( CR CR );
  222.           printf( tempstr );
  223.  
  224. }
  225.  
  226.  
  227.  
  228. void center( char *str )
  229. {
  230.    int padding;
  231.    char st [ LINE_LEN + INCREMENT ];
  232.  
  233.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  234.      memset( st, SPACE, padding );
  235.      *( st + padding ) = NULL;  //Terminate string
  236.      strcat( st, str );
  237.      strcpy( str, st );
  238.  
  239.      return;
  240. }
  241.